package com.alisls.shequtao.service.goods.web;

import com.alisls.shequtao.common.model.result.Result;
import com.alisls.shequtao.service.goods.dto.SpecGroupDTO;
import com.alisls.shequtao.service.goods.dto.SpecParamDTO;
import com.alisls.shequtao.service.goods.query.SpecGroupPageQuery;
import com.alisls.shequtao.service.goods.service.SpecGroupService;
import com.alisls.shequtao.service.goods.service.SpecParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 规格组、规格参数管理
 *
 * @author Ke Wang
 */
@Api(tags = "规格组、规格参数管理")
@RestController
@RequestMapping("/spec")
@AllArgsConstructor
public class SpecController {

    private final SpecGroupService specGroupService;

    private final SpecParamService specParamService;

    @ApiOperation(value = "根据商品分类查询规则组")
    @GetMapping("/group/{categoryId}")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroupsByCategoryId(@PathVariable Long categoryId) {
        List<SpecGroupDTO> specGroups = specGroupService.listSpecGroupsByCategoryId(categoryId);
        return ResponseEntity.ok(specGroups);
    }

    @ApiOperation(value = "根据商品分类分页查询规则组")
    @GetMapping("/group/page")
    public ResponseEntity<Result> pagingSpecGroupsByCategoryId(SpecGroupPageQuery specGroupPageQuery) {
        Result specGroupResult = specGroupService.pagingSpecGroupsByCategoryId(specGroupPageQuery);
        return ResponseEntity.ok(specGroupResult);
    }

    @ApiOperation(value = "根据商品分类ID，规则组ID查询规格参数")
    @GetMapping("/listSpecParamsByCategoryIdAndSpecGroupId")
    public ResponseEntity<List<SpecParamDTO>> listSpecParamsByCategoryIdAndSpecGroupId(
            @RequestParam(value = "categoryId", required = true) Long categoryId,
            @RequestParam(value = "specGroupId", required = true) Long specGroupId) {
        List<SpecParamDTO> specParamDTOs = specParamService.listSpecParamsByCategoryIdAndSpecGroupId(categoryId, specGroupId);
        return ResponseEntity.ok(specParamDTOs);
    }

    @ApiOperation(value = "根据规格组ID查询规格参数")
    @GetMapping("/getSpecParamsBySpecGroupId/{specGroupId}")
    public ResponseEntity<List<SpecParamDTO>> getSpecParamsBySpecGroupId(@PathVariable Long specGroupId) {
        List<SpecParamDTO> specParams = specParamService.getSpecParamsBySpecGroupId(specGroupId);
        return ResponseEntity.ok(specParams);
    }

    @ApiOperation(value = "保存规格组")
    @PostMapping("/group")
    public ResponseEntity<SpecGroupDTO> saveSpecGroupWithCategory(@RequestBody @Valid SpecGroupDTO specGroupDTO) {
        specGroupService.saveSpecGroupWithCategory(specGroupDTO);
        return ResponseEntity.ok(specGroupDTO);
    }

    @ApiOperation(value = "保存规格参数")
    @PostMapping("/saveSpecParamWithSpecGroup")
    public ResponseEntity<SpecParamDTO> saveSpecParamWithSpecGroup(@RequestBody @Valid SpecParamDTO specParamDTO) {
        specParamService.saveSpecParamWithSpecGroup(specParamDTO);
        return ResponseEntity.ok(specParamDTO);
    }

}
